package cn.edu.cdu.group4nb.service.impl;

import cn.edu.cdu.group4nb.mapper.*;
import cn.edu.cdu.group4nb.po.*;
import cn.edu.cdu.group4nb.service.UploadService;
import cn.edu.cdu.group4nb.utils.ReadExcel;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Service
public class UploadServiceImpl implements UploadService {
    @Autowired
    private UserInfMapper userInfMapper;
    @Autowired
    private RoleDistributeMapper roleDistributeMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private InstitutionMapper institutionMapper;
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private EducationMapper educationMapper;
    @Override
    public String ajaxUploadExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        MultipartFile file = multipartRequest.getFile("file");
        if(file.isEmpty()){
            try {
                throw new Exception("文件不存在！");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        InputStream in =null;
        try {
            in = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<List<Object>> listob = null;
        try {
            listob = new ReadExcel().getBankListByExcel(in,file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (int i = 0; i < listob.size(); i++) {
            /*   List<Object> lo = listob.get(i);
               if (lo.get(i)=="") {
                    continue;
                }*/
            System.out.println(listob.get(i));
            UserInf userInf=new UserInf();
            userInf.setUserid((String) listob.get(i).get(0));
            userInf.setPassword(new Md5Hash(listob.get(i).get(1), listob.get(i).get(0), 1024).toHex());
            //把密码进行md5加密存入数据库
            userInf.setName((String) listob.get(i).get(2));
            userInf.setEmail((String) listob.get(i).get(3));
            userInf.setState((String) listob.get(i).get(6));
            userInf.setGithuburl((String) listob.get(i).get(7));
            userInfMapper.insertSelective(userInf);
            int primaryKey=userInf.getUserInfoId();//返回主键
            //自动分配普通员工角色
            RoleDistribute roleDistribute=new RoleDistribute();
            roleDistribute.setRoleid(4);
            roleDistribute.setUserInfoId(primaryKey);
            roleDistribute.setState("0");
            roleDistributeMapper.insert(roleDistribute);
            //得到Positionid
            PositionExample positionExample=new PositionExample();
            PositionExample.Criteria criteria=   positionExample.createCriteria();
            criteria.andNameEqualTo((String) listob.get(i).get(5));
            Position position=   positionMapper.selectByExample(positionExample).get(0);
            //得到educationId
            EducationExample educationExample=new EducationExample();
            EducationExample.Criteria criteria1=educationExample.createCriteria();
            criteria1.andEducationDescEqualTo((String) listob.get(i).get(8));
            Education education = educationMapper.selectByExample(educationExample).get(0);
            //插入record表（为了分配岗位）
            Record record=new Record();
            record.setPositionId(position.getPositionId());
            record.setUserId(userInf.getUserInfoId());
            record.setEducationId(education.getEducationId());
            recordMapper.insertSelective(record);

        }
        return null;
    }
}
